package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.dto.*;
import com.hdax.dm.entity.base.Image;
import com.hdax.dm.entity.item.DmCinema;
import com.hdax.dm.entity.item.DmItem;
import com.hdax.dm.feign.ImageFeign;
import com.hdax.dm.mappers.CinemaMapper;
import com.hdax.dm.mappers.ItemMapper;
import com.hdax.dm.mapstrucs.*;
import com.hdax.dm.service.CinemaService;
import com.hdax.dm.service.ItemService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemSImpl extends ServiceImpl<ItemMapper, DmItem> implements ItemService {

    @Autowired
    private IndexNavDtoMapper indexNavDtoMapper;

    @Autowired
    private FloorDtoMapper floorDtoMapper;

    @Autowired
    private ItemCinemaDtoMapper itemCinemaDtoMapper;

    @Autowired
    private ItemDtoMapper itemDtoMapper;

    @Autowired
    private CinemaService cinemaService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private ImageFeign imageFeign;

    public String ggImageFeign(Long targetId,Long type,Long category){
        //通过openfeign远程调用了base，查询图片信息
        Image image=imageFeign.image(targetId, type, category);
        String imageUrl=image==null?"": image.getImgUrl();
        return imageUrl;
    }

    @Override
    public DmItem itemOne(Long itemId) {
        QueryWrapper<DmItem> wrapper=new QueryWrapper();
        wrapper.eq("id",itemId);
        return getOne(wrapper);
    }

    @Override
    public List<IndexNavDto.HotItems> items(Long itemType1Id, Long isBanner) {
        List<IndexNavDto.HotItems>  dto=indexNavDtoMapper.toTargetsItem(baseMapper.items(itemType1Id, isBanner));
        for (IndexNavDto.HotItems d:dto) {
            d.setImgUrl(ggImageFeign(d.getId(),2L,1L));
        }
        return dto;
    }

    @Override
    public CommonResponse<List<FlootDto>> floorData() {

        List<FlootDto> floorDtos=null;

        if (redisTemplate.opsForValue().get("index:floorData")==null) {
           floorDtos=floorDtoMapper.toTargets(baseMapper.floorData());
            Long cid=null;
            for (FlootDto f : floorDtos) {
                List<DmItem> items= baseMapper.floorItems(f.getItemTypeId());
                for (DmItem item : items) {
                    cid=item.getCinemaId();
                }
                List<ItemCinemaDto> itemCinemaDtos= itemCinemaDtoMapper.toTargets(items);
                for (ItemCinemaDto i : itemCinemaDtos) {
                    DmCinema cinemas= cinemaService.cinemaOne(cid);
                    i.setAreaId(cinemas.getAreaId());
                    i.setAddress(cinemas.getAddress());
                    i.setAreaName(cinemas.getAreaName());
                    i.setImgUrl(ggImageFeign(i.getId(),2L,1L));
                }
                f.setItems(itemCinemaDtos);
            }
            redisTemplate.opsForValue().set("index:floorData",floorDtos);
        }else {
            floorDtos= (List<FlootDto>) redisTemplate.opsForValue().get("index:floorData");
        }
        return ResponseUtil.returnSuccess(floorDtos);
    }

    @Override
    public CommonResponse<List<DmItem>> seniority(Long itemTypeId) {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("itemType1Id",itemTypeId);
        wrapper.eq("isBanner",1L);
        IPage<DmItem> page=page(Page.of(0,12),wrapper);
        return ResponseUtil.returnSuccess(page.getRecords());
    }

    //今日推荐
    @Override
    public CommonResponse<List<ItemDto>> isRecommend() {
        QueryWrapper<DmItem> wrapper=new QueryWrapper<>();
        wrapper.eq("isRecommend",1);
        //分页查询
        IPage<DmItem> page=page(Page.of(0,5),wrapper);
        List<ItemDto> recommendDtos=itemDtoMapper.toTargets((page.getRecords()));
        recommendDtos.forEach(r -> {
            r.setImgUrl(ggImageFeign(r.getId(),2L,1L));
        });
        return ResponseUtil.returnSuccess(recommendDtos);
    }

    //预售
    @Override
    public CommonResponse<List<ItemDto>> sell() {
        QueryWrapper<DmItem> wrapper=new QueryWrapper<>();
        wrapper.eq("state",2);
        //分页查询
        IPage<DmItem> page=page(Page.of(0,5),wrapper);
        List<ItemDto> sellDtos=itemDtoMapper.toTargets(page.getRecords());
        sellDtos.forEach(s -> {
            s.setImgUrl(ggImageFeign(s.getId(),2L,1L));
        });
        return ResponseUtil.returnSuccess(sellDtos);
    }

    @Override
    public CommonResponse<List<ItemCinemaDto>> guesslike(Integer itemTypeId) {
        QueryWrapper<DmItem> wrapper=new QueryWrapper<>();
        wrapper.eq("isRecommend",1);
        if (itemTypeId>0){
            wrapper.eq("itemType1Id",itemTypeId);
        }
        wrapper.last("limit 5");
        List<ItemCinemaDto> recommendDtos=itemCinemaDtoMapper.toTargets(list(wrapper));
        recommendDtos.forEach(r -> {
            r.setImgUrl(ggImageFeign(r.getId(),2L,1L));
        });
        return ResponseUtil.returnSuccess(recommendDtos);
    }
}
